package com.example.neusoft.cook.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoft.cook.Adapter.FoodListAdapter;
import com.example.neusoft.cook.Adapter.ShopFavorListAdapter;
import com.example.neusoft.cook.R;
import com.example.neusoft.cook.ViewHolder.FooditemViewHolder;
import com.example.neusoft.cook.base.BaseFragment;
import com.example.neusoft.cook.bean.SampleCook;
import com.example.neusoft.cook.bean.SampleDeail;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFragment extends BaseFragment {
    private EditText keywordView;
    private Button buttonSearch;
    private RecyclerView recyclerView;
    //搜索结果与菜品列表一致，因此可以复用SampleCook类，FooditemViewHolder类以及FoodListAdapter类
    private FoodListAdapter adapter;

    public SearchFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        keywordView = (EditText) view.findViewById(R.id.editText_keyword);
        buttonSearch = (Button) view.findViewById(R.id.button_search);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        buttonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = keywordView.getText().toString();
                searchFood(keyword);
            }
        });

    }

    private void searchFood(String foodName){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<SampleCook>> call = retrofitService.getFoodBySearch(foodName);
        call.enqueue(new Callback<List<SampleCook>>() {
            @Override
            public void onResponse(Call<List<SampleCook>> call, Response<List<SampleCook>> response) {
                if (getActivity() !=null){
                    adapter = new FoodListAdapter(getActivity(),response.body());
                    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<SampleCook>> call, Throwable t) {
                if (getActivity() != null){
                    Toast.makeText(getActivity(),
                            t.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_search, container, false);
    }

}
